package com.martin.ads.pano360demo;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;

import com.martin.ads.vrlib.constant.AdjustingMode;
import com.martin.ads.vrlib.filters.base.DrawImageFilter;
import com.martin.ads.vrlib.filters.base.FilterGroup;
import com.martin.ads.vrlib.filters.vr.RGBFilter;
import com.martin.ads.vrlib.utils.BitmapUtils;
import com.martin.ads.vrlib.utils.TextImageGenerator;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

/**
 * @author lejun
 * @Description:
 */
public class TestActivity extends AppCompatActivity {

    private FilterGroup filterGroup;

    public static void startActivity(Context context) {
        Intent intent = new Intent(context, TestActivity.class);
        context.startActivity(intent);
    }

    GLSurfaceView glSurfaceView;
    private RGBFilter rgbFilter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
        glSurfaceView = (GLSurfaceView) findViewById(R.id.gl_surface);

        glSurfaceView.setEGLContextClientVersion(2);
        MyRender renderer = new MyRender();
        glSurfaceView.setRenderer(renderer);
        glSurfaceView.setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);


        Bitmap bitmap1 = BitmapUtils.loadBitmapFromRaw(this, R.raw.ic_launcher);
        Bitmap bitmap = BitmapUtils.loadBitmapFromAssets(this, "images/texture_360_n.jpg");
        Bitmap bitmap2 = TextImageGenerator.newInstance()
                .setTextColor(Color.WHITE)
                .setTextSize(30)
                .addTextToImage("这是一个好天气，应该好好珍惜哈。是的，那就出动全部的热情。。。。哈哈是这一个很长的字串");

        byte[] pixelsBGRA = BitmapUtils.getPixelsBGRA(bitmap2);
        int[] ints = BitmapUtils.convertByteToColor(pixelsBGRA);
        Bitmap bitmap3 = BitmapUtils.rgb2Bitmap(ints, bitmap2.getWidth(), bitmap2.getHeight());

        DrawImageFilter drawImageFilter = new DrawImageFilter(this, bitmap3, AdjustingMode.ADJUSTING_MODE_WRAP);

        filterGroup = new FilterGroup();
//        IconHotspot iconHotspot = IconHotspot.with(this)
//                .setPositionOrientation(PositionOrientation.newInstance().setX(0.5f).setY(0.5f))
//                .setBitmap(bitmap1);
////
//        IconHotspot iconHotspot2 = IconHotspot.with(this)
//                .setPositionOrientation(PositionOrientation.newInstance().setX(0f).setY(0.5f))
//                .setBitmap(bitmap);
//
//        HotsportContainer hotsportContainer = new HotsportContainer(this);
//        List<AbsHotspot> absHotspots = new ArrayList<>();
//
//        absHotspots.add(iconHotspot2);
//        absHotspots.add(iconHotspot);
//
//        hotsportContainer.setHotspotList(absHotspots);
//
//        filterGroup.addFilter(drawImageFilter);
//        filterGroup.addFilter(hotsportContainer);
//        byte[] pixelsBGRA1 = BitmapUtils.getPixelsBGRA(bitmap1);
//        int[] floats = BitmapUtils.convertByteToColor(pixelsBGRA1);
        byte[] floats  = BitmapUtils.getPixelsRGBA(bitmap1);
        rgbFilter = new RGBFilter(this, floats, bitmap1.getWidth(), bitmap1.getHeight());
        filterGroup.addFilter(rgbFilter);
    }


    public class MyRender implements GLSurfaceView.Renderer {

        @Override
        public void onSurfaceCreated(GL10 gl, EGLConfig config) {
            filterGroup.init();

        }

        @Override
        public void onSurfaceChanged(GL10 gl, int width, int height) {
            filterGroup.onFilterChanged(width, height);
        }

        @Override
        public void onDrawFrame(GL10 gl) {
            filterGroup.onDrawFrame(0);
        }
    }
}
